package inferenceengine;

import java.util.ArrayList;
import java.util.List;

import commondata.IPlayer;
import commondata.ITradeKnowledge;
import commondata.ITradePackage;
import commondata.TradeOptionStruct;
import commondata.TradePackage;

public class TradeKnowledge implements ITradeKnowledge{

	public String getTradeGainForFirstTeam(TradeOptionStruct tradeOptionStruct) {
		List<String> result = new ArrayList<String>();
		String message ="";
		
		ITradePackage outgoinPackage = createTradePackage(tradeOptionStruct.getFirstTeamPlayers());
		ITradePackage incomingPackage = createTradePackage(tradeOptionStruct.getSecondTeamPlayers());
		
		if(incomingPackage.getPointPG() > outgoinPackage.getPointPG()){
			result.add("Points by %"+getPercentage(incomingPackage.getPointPG(),outgoinPackage.getPointPG())+"\n");
		}
		if(incomingPackage.getReboundPG() > outgoinPackage.getReboundPG()){
			result.add("Rebounds by %"+getPercentage(incomingPackage.getReboundPG(),outgoinPackage.getReboundPG())+"\n");
		}
		if(incomingPackage.getAssistPG() > outgoinPackage.getAssistPG()){
			result.add("Assists by %"+getPercentage(incomingPackage.getAssistPG(),outgoinPackage.getAssistPG())+"\n");
		}
		if(incomingPackage.getBlockPG() > outgoinPackage.getBlockPG()){
			result.add("Blocks by %"+getPercentage(incomingPackage.getBlockPG(),outgoinPackage.getBlockPG())+"\n");
		}
		if(incomingPackage.getStealPG() > outgoinPackage.getStealPG()){
			result.add("Steals by %"+getPercentage(incomingPackage.getStealPG(),outgoinPackage.getStealPG())+"\n");
		}
		if(result.size() > 0){
			message = tradeOptionStruct.getFirstTeam().getTeamName() +" has gain on the following statistics\n";
			for(String statistics : result){
				message +=statistics;
			}
		}
		return message;
	}

	private double getPercentage(double inPG, double outPG) {
		double diff = inPG - outPG;
		if(diff == 0)
			return 0;
		
		if(outPG == 0)
			return 100;
		return formatDouble(diff * 100 / outPG);
	}

	private ITradePackage createTradePackage(List<IPlayer> playerList) {

		double pointsPG = 0;
		double reboundPG = 0;
		double blockPG = 0;
		double stealPG = 0;
		double assistPG = 0;
		
		for (IPlayer pacplayer : playerList) {
			pointsPG += pacplayer.getPointPG();
			reboundPG += pacplayer.getReboundPG();
			blockPG += pacplayer.getBlockPG();
			stealPG += pacplayer.getStealPG();
			assistPG += pacplayer.getAssistPG();
		}
		int size = playerList.size();
		ITradePackage tradePackage = new TradePackage(playerList, pointsPG
				/ size, reboundPG / size, blockPG / size, assistPG / size,
				stealPG / size);
		return tradePackage;
	}
	
	double formatDouble(double d){
		int in = (int) (d * 100);
		return ((double)in / 100);
	}

}
